package com.study.mongodbdynamicdatasource.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;

/**
 * @Name: MultiMongoConfig
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2023/7/20 15:47
 * @Remark: 特殊说明
 * @Version: 1.0
 */
@Configuration
@Slf4j
public class MultiMongoConfig {
	@Bean(name = "mongoDatabaseFactoryLoader")
	@ConditionalOnMissingBean(IMongoDatabaseFactoryLoader.class)
	public IMongoDatabaseFactoryLoader mongoDatabaseFactoryLoader(MongoListProperties mongoListProperties){
		log.info("PropertyMongoDbFactoryLoader");
		return new PropertyMongoDbFactoryLoader(mongoListProperties);
	}

	@Bean(name = "mongoTemplate")
	public MultiMongoTemplate dynamicMongoTemplate(IgnoreCollectionProperties ignoreCollectionProperties, MongoDatabaseFactory mongoDatabaseFactory,IMongoDatabaseFactoryLoader mongoDatabaseFactoryLoader) throws Exception {
		System.out.println(mongoDatabaseFactoryLoader.toString());
		MongoDbContext.loadMongoClientDbFactory(mongoDatabaseFactoryLoader.loadMongoClientDbFactory());
		return new MultiMongoTemplate(ignoreCollectionProperties,mongoDatabaseFactory);
	}
}
